import face_recognition
import os

# 加载目标人脸图像
target_image = face_recognition.load_image_file("D:/文档/个人档案/IMG_6285(20231103-153758).JPG")
target_encoding = face_recognition.face_encodings(target_image)[0]

# 目标目录，查找图片文件（递归）
search_directory = "D:/文档/"

# 支持的图片格式
supported_formats = (".jpg", ".jpeg", ".png")

# 递归遍历文件夹
for root, dirs, files in os.walk(search_directory):
    for file in files:
        if file.endswith(supported_formats):
            image_path = os.path.join(root, file)

            # 加载图片并获取人脸编码
            image = face_recognition.load_image_file(image_path)
            encodings = face_recognition.face_encodings(image)

            # 对比每张图片中的人脸编码
            for encoding in encodings:
                matches = face_recognition.compare_faces([target_encoding], encoding)
                if True in matches:
                    print(f"找到匹配人脸的图片: {image_path}")

